package algorithmcase;

import java.util.Arrays;
import java.util.Iterator;

public class Case6 {
    //插入排序
    public static void main(String[] args) {
        int[] arr = {21,22,30,40,36,13,19,16,20,7,10,43,50,58};
        int startIndex = -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > arr[i+1]){
                //找到无序开始的坐标
                startIndex = i+1;
                break;
            }
        }
        //从无序坐标开始遍历
        for(int i = startIndex;i<arr.length;i++){
            int j =i;
            while(j>0 && arr[j] < arr[j-1]){
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;
                j--;
            }
        }
        //递归调用
        System.out.println(getSum(5));

        System.out.println(Arrays.toString(arr));

        //迭代器
        Iterator<Integer> it = Arrays.stream(arr).iterator();
        //使用迭代器遍历数组,hasNext方法：判断当前指针所指区域是否有元素
        //next方法：获取当前指针所指区域的元素,并将指针移动到下一个位置
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
    public static int getSum(int num){
        if(num == 1){
            return 1;
        }
        return num + getSum(num-1);
    }
}
